
package com.eyedog.video.NotifyDialog;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;

import com.eyedog.uilibrary.base.BaseDialog;
import com.eyedog.video.entity.NotifyEntity;

public class NotifyDialog extends BaseDialog implements constract.INotifyView {

    NotifyEntity mNotifyEntity;

    NotifyPresenter mPresenter;

    public NotifyDialog(@NonNull Context context) {
        super(context);
    }

    public NotifyDialog(@NonNull Context context, int theme) {
        super(context, theme);
    }

    protected NotifyDialog(@NonNull Context context, boolean cancelable,
            @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new NotifyPresenter(this, new NotifyModel());
        mPresenter.displayNotify();
        mTitle.setText("通知");
        mCancel.setVisibility(View.GONE);
        mConfirm.setText("关闭公告");
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mNotifyEntity != null && !TextUtils.isEmpty(mNotifyEntity.url)) {
                    String url = mNotifyEntity.url;
                    Uri uri = Uri.parse(url);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    getContext().startActivity(intent);
                }
                dismiss();
            }
        });
        if (mNotifyEntity != null) {
            showNotify(mNotifyEntity);
        }
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    @Override
    public void showNotify(NotifyEntity notifyEntity) {
        this.mNotifyEntity = notifyEntity;
        if (mContent != null && mNotifyEntity != null) {
            mContent.post(new Runnable() {
                @Override
                public void run() {
                    if (!TextUtils.isEmpty(mNotifyEntity.url)) {
                        mCancel.setVisibility(View.VISIBLE);
                        mConfirm.setVisibility(View.VISIBLE);
                        mCancel.setText("关闭公告");
                        mConfirm.setText("打开链接");
                    }
                    if (!TextUtils.isEmpty(mNotifyEntity.content)) {
                        mContent.setText(getClickableHtml(mNotifyEntity.content));
                        mContent.setMovementMethod(LinkMovementMethod.getInstance());
                    }
                }
            });
        }
    }

    private void setLinkClickable(final SpannableStringBuilder clickableHtmlBuilder,
            final URLSpan urlSpan) {
        int start = clickableHtmlBuilder.getSpanStart(urlSpan);
        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
        ClickableSpan clickableSpan = new ClickableSpan() {

            public void onClick(View view) {
                // Do something with URL here.
                String url = urlSpan.getURL();
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                getContext().startActivity(intent);
            }
        };
        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
    }

    private CharSequence getClickableHtml(String html) {
        Spanned spannedHtml = Html.fromHtml(html);
        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
        for (final URLSpan span : urls) {
            setLinkClickable(clickableHtmlBuilder, span);
        }
        return clickableHtmlBuilder;
    }
}
